/*
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package br.com.insejeccg.util.bean.convert;

import java.util.Date;

import org.apache.commons.validator.GenericTypeValidator;

import br.com.insejeccg.util.bean.formatter.Formatter;

@SuppressWarnings("unchecked")
public class DateConverter implements ConverterToString {
    private String defaultPattern;
    private Object defaultValue;
    private String typeName;

    public DateConverter(String pattern, String typeName,
        Object defaultValue) {
        super();
        this.defaultPattern = pattern;
        this.defaultValue = defaultValue;
        this.typeName = typeName;
    }

    public Object convert(Class type, Object value) {
        if (value == null || value.toString().trim().equals("")) {
            return (defaultValue);
        }

        if (type.isInstance(value)) {
            return value;
        }

        try {
            String pattern = getPattern(this.typeName, this.defaultPattern);
            Date resp = null;

            if (value instanceof Date) {
                resp = (Date) value;
            } else {
                resp = GenericTypeValidator.formatDate(value.toString(), pattern, true);
            }

            if (type.equals(java.sql.Date.class)) {
                resp = new java.sql.Date(resp.getTime());
            } else if (type.equals(java.sql.Time.class)) {
                resp = new java.sql.Time(resp.getTime());
            } else if (type.equals(java.sql.Timestamp.class)) {
                resp = new java.sql.Timestamp(resp.getTime());
            } 

            return resp;
        } catch (Exception e) {
            return (defaultValue);
        }
    }

    public String convertToString(Object value) {
        if ((value != null) && value instanceof Date) {
            try {
                String pattern = getPattern(this.typeName, this.defaultPattern);
                return Formatter.format((Date)value, pattern);
            } catch (Exception e) {
                return "";
            }
        }

        return "";
    }
  
    private String getPattern(String key, String defaultPattern) {        
        String pattern = Formatter.getResourcePattern(key);
        if(pattern != null) {
        	return pattern;	
		}
        return defaultPattern;
    }
    public static void main(String[] args) {
		System.out.println(GenericTypeValidator.formatDate("25/03/2007", "dd/MM/yyyy", true));
	}
	
}
